home *** CD-ROM | disk | FTP | other *** search
-
- Network Daemons Programming Reference
- -------------------------------------
- In order for the DESQview/X Network Manager to process certain
- service requests, a daemon (that is, unseen) process may be
- started to handle the request. This section describes the
- programming interface provided for developers to create these
- network daemons.
-
- To configure the DESQview/X Network Manager to use a new daemon,
- see the Network Manager Configuration section for details.
-
- Note that network daemons use the Berkeley Socket Interface to
- communicate with the DESQview/X Network Manager (see that section
- for details). In addition, DESQview/X supplies extension routines
- to facilitate the creation of new daemons.
-
-
- Daemon Types
- ------------
- There are two type of daemons that may be created for use in a
- DESQview/X system - regular and lingering daemons:
-
- Regular These kinds of daemons are started by the Network
- Manager, process the supplied request and then
- terminate immediately.
-
- Lingering A lingering daemon is one that also processes the
- supplied request, but instead of terminating
- immediately,"lingers" for a specified period of
- time in anticipation of another service request.
- If no service request is accepted by the Network
- Manager before the linger time expires, the daemon
- is terminated.
-
- Note that each daemon type may appear in one of two flavors - the
- daemon may be a TCP (stream) daemon or a UDP (datagram) daemon.
-
-
- General Operation
- -----------------
- The interaction between the Network Manager and a network daemon
- is as follows:
-
- The Network Manager accepts a request for a particular
- service that has a daemon associated with it.
-
- If the daemon is not currently running, the Network Manager
- instructs DESQview/X to start the daemon and passes the
- service request to it.
-
- For daemons already running, the Network Manager forwards the
- service request directly to the daemon.
-
- When the daemon has processed the request, it either
- terminates or lingers for a specified period of time.
-
-
- Anatomy of a Daemon
- -------------------
- In order to facilitate the communication between the DESQview/X
- Network Manager and a daemon, DESQview/X supplies extension
- routines (listed at the end of this section). The most important
- of these routines is the so_daemon call - this encapsulates a lot
- of handshaking and passing of parameters between the Network
- Manager and the daemon and frees the daemon writer from the
- tedium of performing a lot of work.
-
- A regular daemon will normally use the so_daemon and
- so_exitdaemon routines by following this model:
-
- #include <netdb.h>
- #include <netinet\in.h>
- #include <sys\socket.h>
-
- void main(int argc,char *argv[])
- {
- int s;
-
- /* ... main work ... */
-
- if((s = so_daemon(argv,FALSE)) >= 0) {
- perform_bsd_socket_work();
- so_close(s); /* for TCP daemons ... or ... */
- so_unlink(s); /* for UDP daemons */
- }
- so_exitdaemon();
- }
-
- A lingering daemon will normally use the so_daemon and
- so_exitdaemon routines by following this model:
-
- #include <netdb.h>
- #include <netinet\in.h>
- #include <sys\socket.h>
-
- void main(int argc,char *argv[])
- {
- int s;
-
- /* ... main loop ... */
-
- while (1) {
- if((s = so_daemon(argv,TRUE)) <0)
- continue;
- perform_bsd_socket_work();
- so_close(s); /* for TCP daemons ... or ... */
- so_unlink(s); /* for UDP daemons */
- }
- }
-
- Note that the so_daemon routine handles the linger time for the
- daemon and will terminate the daemon if it is exceeded.
-
- Differences between TCP and UDP Daemons
- ---------------------------------------
- There are several differences between a TCP and a UDP daemon that
- should be noted.
-
- A TCP daemon will use the so_close routine to close a socket.
-
- A UDP daemon must use the so_unlink extension routine to
- release a socket. This is because UDP daemons are handed a
- child socket that was generated when a request for the
- service was made. Closing the socket with so_close would
- prevent further requests for that service being processed by
- the Network Manager.
-
- A TCP daemon will use TCP I/O primitives, such as recv and
- send in the main body of the daemon (perform_bsd_socket_work)
- to perform the required communication.
-
- A UDP daemon will use UDP I/O primitives, such as recvfrom
- and sendto in the main body of the daemon
- (perform_bsd_socket_work) to perform the required
- communication.
-
- Daemon Routines
- ---------------
- The DESQview/X extension routines that enable network daemons to
- be created with a minimum of effort are now listed in reference
- form:
-
-
- so_daemon()
- ***********
-
- Synopsis
- --------
- #include <sys\socket.h>
-
- int so_daemon(char *args[],char wait);
-
- Description
- -----------
- The so_daemon call is the linkup call for DESQview/X daemon
- processes. The call retrieves the connection information for the
- next connection to be processed by the daemon. The args parameter
- must be the arguments passed to the application upon entry by the
- C runtime library. The wait parameter allows the daemon to
- optionally poll for additional connections or datagrams, while
- continuing to process current connections.
-
- As implied above, the call can be made for daemons of both type
- SOCK_STREAM and SOCK_DGRAM. For SOCK_STREAM daemons, the
- descriptor returned can be processed normally and closed with a
- standard so_close call. For SOCK_DGRAM daemons, the descriptor
- is a datagram descriptor ready to make a recvfrom call. The
- descriptor should then be closed via the so_unlink call.
-
- When the wait parameter is positive, the so_daemon
- call will perform all of the duties of determining the proper linger
- period and exiting when the time has been exceeded before the next
- request arrives.
-
- Notes
- -----
- DESQview/X extension call.
-
- Return Values
- -------------
- so_daemon returns a non-negative descriptor on success and -1 on
- failure.
-
-
- so_exitdaemon()
- ***************
-
- Synopsis
- --------
- #include <sys\socket.h>
-
- void so_exitdaemon(void);
-
- Description
- -----------
- The so_exitdaemon call should be made before the daemon process
- terminates. This will allow the socket interface to be
- uninitialized correctly.
-
- Notes
- -----
- DESQview/X extension call.
-
-
- so_unlink()
- ***********
-
- Synopsis
- --------
- #include <sys\socket.h>
-
- int so_unlink(int s);
-
- Description
- -----------
- The so_unlink call returns ownership of a daemon socket to the
- DESQview/X Network Manager. This call is provided for SOCK_DGRAM
- type sockets so as to allow them to continue pending after
- termination of a daemon process. This call must be used in place
- of the so_close call for datagram daemons.
-
- Notes
- -----
- DESQview/X extension call.
-
- Return Values
- -------------
- so_unlink returns 0 on success, -1 on failure (errno as
- described below).
-
- Errors
- ------
- ENOTSOCK Invalid descriptor.
-
-